/*
 * Created by JFormDesigner on Wed May 29 18:43:07 CST 2024
 */

package view.user;

import model.booking.Booking;
import model.manager.Manager;

import java.awt.event.*;
import java.time.LocalDate;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author 睡鼠
 */
public class ContinuationOfResidence extends JPanel {
    private LocalDate startTime1;
    private LocalDate endTime1;
    private LocalDate newEndTime;
    private Manager manager;
    public ContinuationOfResidence(Manager manager) {
        this.manager=manager;
        initComponents();
        initializeComboBoxes();
    }

    private void Confrim(ActionEvent e) {
        // TODO add your code here
    }
    //续住的确认按钮
    private void ContinuationResidenceButton(ActionEvent e) {
        // TODO add your code here
 

    }

    private void button1(ActionEvent e) {
        // TODO add your code here
    }

    private void EnSureTime(ActionEvent e) {
        // TODO add your code here
        String year = Integer.toString((Integer) this.year.getSelectedItem());
        String month = Integer.toString((Integer) this.month.getSelectedItem());
        String day = Integer.toString((Integer) this.day.getSelectedItem());
        String year2 = Integer.toString((Integer) this.year2.getSelectedItem());
        String month2 = Integer.toString((Integer) this.month2.getSelectedItem());
        String day2 = Integer.toString((Integer) this.day2.getSelectedItem());
        String year3 = Integer.toString((Integer) this.year3.getSelectedItem());
        String month3 = Integer.toString((Integer) this.month3.getSelectedItem());
        String day3 = Integer.toString((Integer) this.day3.getSelectedItem());
        startTime1 = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        endTime1 = LocalDate.of(Integer.parseInt(year2), Integer.parseInt(month2), Integer.parseInt(day2));
        newEndTime =LocalDate.of(Integer.parseInt(year3), Integer.parseInt(month3), Integer.parseInt(day3));
        //向管理员端传入参数，执行确认入住操作
        if (manager.dealWithExtend(NametextField1.getText(),IdtextField2.getText(),startTime1,endTime1,newEndTime)) {
            JOptionPane.showMessageDialog(this, "续住成功");
        }else {
            JOptionPane.showMessageDialog(this, "续住失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - Qiqq
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        NametextField1 = new JTextField();
        IdtextField2 = new JTextField();
        panel1 = new JPanel();
        year = new JComboBox();
        month = new JComboBox();
        day = new JComboBox();
        label5 = new JLabel();
        panel2 = new JPanel();
        year2 = new JComboBox();
        month2 = new JComboBox();
        day2 = new JComboBox();
        label6 = new JLabel();
        panel3 = new JPanel();
        year3 = new JComboBox();
        month3 = new JComboBox();
        day3 = new JComboBox();
        label7 = new JLabel();
        button1 = new JButton();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border.
        EmptyBorder( 0, 0, 0, 0) , "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e", javax. swing. border. TitledBorder. CENTER, javax. swing
        . border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069al\u006fg" ,java .awt .Font .BOLD ,12 ),
        java. awt. Color. red) , getBorder( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( )
        { @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062or\u0064er" .equals (e .getPropertyName () ))
        throw new RuntimeException( ); }} );

        //---- label1 ----
        label1.setText("\u7528\u6237\u7eed\u4f4f\u4fe1\u606f");

        //---- label2 ----
        label2.setText("\u7528\u6237\u59d3\u540d\uff1a");

        //---- label3 ----
        label3.setText("\u7528\u6237\u8eab\u4efd\u53f7\uff1a");

        //---- label4 ----
        label4.setText("\u65e5\u671f\u786e\u8ba4\uff1a");

        //======== panel1 ========
        {

            //---- label5 ----
            label5.setText("\u5165\u4f4f\u65e5\u671f");

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(year, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(month, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(day, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(day, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(month, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                            .addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }

        //======== panel2 ========
        {

            //---- label6 ----
            label6.setText("\u9000\u623f\u65e5\u671f");

            GroupLayout panel2Layout = new GroupLayout(panel2);
            panel2.setLayout(panel2Layout);
            panel2Layout.setHorizontalGroup(
                panel2Layout.createParallelGroup()
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(year2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(month2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(day2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
            );
            panel2Layout.setVerticalGroup(
                panel2Layout.createParallelGroup()
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(day2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(month2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                            .addComponent(year2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }

        //======== panel3 ========
        {

            //---- label7 ----
            label7.setText("\u65b0\u9000\u623f\u65e5\u671f");

            GroupLayout panel3Layout = new GroupLayout(panel3);
            panel3.setLayout(panel3Layout);
            panel3Layout.setHorizontalGroup(
                panel3Layout.createParallelGroup()
                    .addGroup(panel3Layout.createSequentialGroup()
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label7, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(year3, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(month3, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(day3, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
            );
            panel3Layout.setVerticalGroup(
                panel3Layout.createParallelGroup()
                    .addGroup(panel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(day3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(month3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                            .addComponent(year3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }

        //---- button1 ----
        button1.setText("\u786e\u8ba4\u7eed\u4f4f");
        button1.addActionListener(e -> EnSureTime(e));

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGap(92, 92, 92)
                    .addGroup(layout.createParallelGroup()
                        .addComponent(label3)
                        .addComponent(label2)
                        .addComponent(label4))
                    .addGroup(layout.createParallelGroup()
                        .addGroup(layout.createSequentialGroup()
                            .addGap(43, 43, 43)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
                                .addComponent(NametextField1, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                .addComponent(IdtextField2, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup()
                                .addComponent(panel2, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE)
                                .addComponent(panel1, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE)
                                .addComponent(panel3, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(160, Short.MAX_VALUE))
                .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(562, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(NametextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3)
                        .addComponent(IdtextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup()
                        .addComponent(label4)
                        .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(panel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(panel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addContainerGap())
        );
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - Qiqq
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField NametextField1;
    private JTextField IdtextField2;
    private JPanel panel1;
    private JComboBox year;
    private JComboBox month;
    private JComboBox day;
    private JLabel label5;
    private JPanel panel2;
    private JComboBox year2;
    private JComboBox month2;
    private JComboBox day2;
    private JLabel label6;
    private JPanel panel3;
    private JComboBox year3;
    private JComboBox month3;
    private JComboBox day3;
    private JLabel label7;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on

    public void initializeComboBoxes() {
        // 初始化年份下拉框内容
        year.removeAllItems();
        for (int i = 2024; i <= 2034; i++) {
            year.addItem(i);
            year2.addItem(i);
            year3.addItem(i);
        }

        // 初始化月份下拉框内容
        month.removeAllItems();
        for (int i = 1; i <= 12; i++) {
            month.addItem(i);
            month2.addItem(i);
            month3.addItem(i);
        }

        // 初始化日期下拉框内容，确保只显示入住日期之后的日期
        updateDaysWithCheckInDate();

        // 为年份和月份下拉框添加事件监听器
        ActionListener updateDaysActionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateDaysWithCheckInDate();
            }
        };

        year.addActionListener(updateDaysActionListener);
        month.addActionListener(updateDaysActionListener);
    }

    public void updateDaysWithCheckInDate() {
        if (year.getSelectedItem() == null || month.getSelectedItem() == null) {
            return;
        }
        int selectedYear = (int) year.getSelectedItem();
        int selectedMonth = (int) month.getSelectedItem();

        // 由于Calendar月份是从0开始的，所以在获取月份时需要减1
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, selectedYear);
        calendar.set(Calendar.MONTH, selectedMonth - 1);

        int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        day.removeAllItems();
        // 只添加入住日期之后的日期
        for (int i = 1; i <= maxDayOfMonth; i++) {
            day.addItem(i);
            day2.addItem(i);
            day3.addItem(i);

        }
    }
    //判断startTime endTime是否合理
    public boolean isValidDate(LocalDate startTime,LocalDate endTime) {
        if (startTime.isAfter(endTime)) {
            JOptionPane.showMessageDialog(null, "请选择正确的日期");
            return false;
        }else if (startTime.isEqual(endTime)) {
            JOptionPane.showMessageDialog(null, "请选择正确的日期");
            return false;
        } else {
            return true;
        }
    }
}
